package process.module;

import java.io.File;

import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLAxiom;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLIndividual;
import org.semanticweb.owlapi.model.OWLObjectProperty;
import org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.semanticweb.owlapi.model.OWLOntologyStorageException;

import core.constant.SystemConstant;

public class OntologyBuilder {

	public static final String hasManufacturer = "hasManufacturer";

	private OWLOntologyManager manager;
	private OWLDataFactory factory;
	private IRI ontologyIRI;
	private OWLOntology ontology;

	public OntologyBuilder(String filePath)
			throws OWLOntologyCreationException, OWLOntologyStorageException {
		// --- Load an existed file and write ---
		manager = OWLManager.createOWLOntologyManager();
		ontologyIRI = IRI.create(SystemConstant.NS);
		File file = new File(filePath);
		try {
			ontology = manager.loadOntologyFromOntologyDocument(file);
			factory = manager.getOWLDataFactory();
		} catch (OWLOntologyCreationException e) {
			System.out.println(e);
		}

		// --- Create a new file ---
		// manager = OWLManager.createOWLOntologyManager();
		// factory = manager.getOWLDataFactory();
		//
		// ontologyIRI = IRI.create(IRIName);
		// IRI physicalURI = IRI.create(filePath);
		// SimpleIRIMapper mapper = new SimpleIRIMapper(ontologyIRI,
		// physicalURI);
		// manager.addIRIMapper(mapper);
		//
		// ontology = manager.createOntology(ontologyIRI);
		// manager.saveOntology(ontology);
	}

	public OWLClass addTopClass(String className)
			throws OWLOntologyStorageException {
		OWLClass newClass = factory.getOWLClass(IRI.create(ontologyIRI
				+ className));
		OWLAxiom axiom = factory.getOWLDeclarationAxiom(newClass);
		manager.addAxiom(ontology, axiom);
		manager.saveOntology(ontology);
		return newClass;
	}

	public OWLClass addSubClass(String className, OWLClass superClass)
			throws OWLOntologyStorageException {
		OWLClass newClass = factory.getOWLClass(IRI.create(ontologyIRI
				+ className));
		OWLAxiom axiom = factory.getOWLSubClassOfAxiom(newClass, superClass);
		manager.addAxiom(ontology, axiom);
		manager.saveOntology(ontology);
		return newClass;
	}

	public OWLIndividual addIndividual(String individualName,
			OWLClass superClass) throws OWLOntologyStorageException {
		OWLIndividual newIndividual = factory.getOWLNamedIndividual(IRI
				.create(ontologyIRI + individualName));
		OWLAxiom axiom = factory.getOWLClassAssertionAxiom(superClass,
				newIndividual);
		manager.addAxiom(ontology, axiom);
		manager.saveOntology(ontology);
		return newIndividual;
	}

	public OWLIndividual addIndividual(String individualName, String superClass)
			throws OWLOntologyStorageException {
		OWLIndividual newIndividual = factory.getOWLNamedIndividual(IRI
				.create(ontologyIRI + individualName));
		OWLClass sClass = factory.getOWLClass(IRI.create(ontologyIRI
				+ superClass));
		OWLAxiom axiom = factory.getOWLClassAssertionAxiom(sClass,
				newIndividual);
		manager.addAxiom(ontology, axiom);
		manager.saveOntology(ontology);
		return newIndividual;
	}

	public OWLIndividual addIndividual(String individualName,
			String superClass, String propertyValue)
			throws OWLOntologyStorageException {
		// Create new Instance
		OWLIndividual newIndividual = factory.getOWLNamedIndividual(IRI
				.create(ontologyIRI + individualName));

		// Add superclass to Instance
		OWLClass sClass = factory.getOWLClass(IRI.create(ontologyIRI
				+ superClass));
		OWLAxiom axiom = factory.getOWLClassAssertionAxiom(sClass,
				newIndividual);

		// Create a Object property
		OWLObjectProperty property = factory.getOWLObjectProperty(IRI
				.create(ontologyIRI + hasManufacturer));

		// Add Object Property to Instace
		OWLIndividual pro = factory.getOWLNamedIndividual(IRI
				.create(ontologyIRI + propertyValue));
		OWLObjectPropertyAssertionAxiom assertionAxiom = factory
				.getOWLObjectPropertyAssertionAxiom(property, newIndividual,
						pro);

		manager.addAxiom(ontology, axiom);
		manager.addAxiom(ontology, assertionAxiom);
		manager.saveOntology(ontology);
		return newIndividual;
	}

	public OWLOntologyManager getManager() {
		return manager;
	}

	public void setManager(OWLOntologyManager manager) {
		this.manager = manager;
	}

	public OWLDataFactory getFactory() {
		return factory;
	}

	public void setFactory(OWLDataFactory factory) {
		this.factory = factory;
	}

	public IRI getOntologyIRI() {
		return ontologyIRI;
	}

	public void setOntologyIRI(IRI ontologyIRI) {
		this.ontologyIRI = ontologyIRI;
	}

	public OWLOntology getOntology() {
		return ontology;
	}

	public void setOntology(OWLOntology ontology) {
		this.ontology = ontology;
	}

	public static void main(String[] args) throws Exception {
		OntologyBuilder builder = new OntologyBuilder(
				"C:\\Users\\PH\\Desktop\\Gate Test\\abc.rdf");
		builder.addIndividual("40D", "Camera", "Canon");
		System.out.println("DONE!!!!!!!!");
	}
}
